﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GzipManagerPro.IO;

namespace GzipManagerPro.UI.Utility
{
    /// <summary>
    /// Standard .NET FileSystemInfos are always not equal to each other.
    /// When we try to refresh a directory, our controls can't match up new
    /// files with existing files. They are also sealed so we can't just subclass them.
    /// This class is a wrapper around a FileSystemInfo that simply provides
    /// equality.
    /// </summary>
    public class ArchiveFileSystemInfo : IEquatable<ArchiveFileSystemInfo>
    {
        public ArchiveFileSystemInfo(ArchiveItem item)
        {
            if (item == null) throw new ArgumentNullException("fileSystemInfo");
            this._Info = item;
        }

        public bool IsDirectory { get { return this._Info is Folder; } }

        //public DirectoryInfo AsDirectory { get { return info as DirectoryInfo; } }
        //public FileInfo AsFile { get { return info as FileInfo; } }

        public ArchiveItem Info
        {
            get { return this._Info; }
        }

        private readonly ArchiveItem _Info;

        public IEnumerable GetFileSystemInfos()
        {
            ArrayList children = new ArrayList();
            if (this.IsDirectory)
            {
                Folder folder = this._Info as Folder;
                foreach (ArchiveItem item in folder.Folders)
                    children.Add(new ArchiveFileSystemInfo(item));

                foreach (ArchiveItem item in folder.Files)
                    children.Add(new ArchiveFileSystemInfo(item));
            }

            //children.Sort(new FileDirectoryComparer());

            return children;
        }

        public string Name
        {
            get { return _Info.Name; }
        }

        public string Extension
        {
            get { return null; }
        }

        

        
        public string FullName
        {
            get { return _Info.Name; }
        }

        public long Length
        {
            get
            {
                if(this.IsDirectory) return 0;
                return (this._Info as GzipManagerPro.IO.File).Length;
            }
        }

        //public IEnumerable GetFileSystemInfos()
        //{
        //    ArrayList children = new ArrayList();
        //    if (this.IsDirectory)
        //    {
        //        foreach (FileSystemInfo x in this.AsDirectory.GetFileSystemInfos())
        //            children.Add(new CustomFileSystemInfo(x));
        //    }

        //    children.Sort(new FileDirectoryComparer());

        //    return children;
        //}

        // Two file system objects are equal if they point to the same file system path

        //public bool Equals(CustomFileSystemInfo other)
        //{
        //    if (ReferenceEquals(null, other)) return false;
        //    if (ReferenceEquals(this, other)) return true;
        //    return Equals(other.info.FullName, this.info.FullName);
        //}
        //public override bool Equals(object obj)
        //{
        //    if (ReferenceEquals(null, obj)) return false;
        //    if (ReferenceEquals(this, obj)) return true;
        //    if (obj.GetType() != typeof(CustomFileSystemInfo)) return false;
        //    return Equals((CustomFileSystemInfo)obj);
        //}
        //public override int GetHashCode()
        //{
        //    return (this.info != null ? this.info.FullName.GetHashCode() : 0);
        //}
        //public static bool operator ==(CustomFileSystemInfo left, CustomFileSystemInfo right)
        //{
        //    return Equals(left, right);
        //}
        //public static bool operator !=(CustomFileSystemInfo left, CustomFileSystemInfo right)
        //{
        //    return !Equals(left, right);
        //}

        public bool Equals(ArchiveFileSystemInfo other)
        {
            return false;
        }
    }

    //public class FileDirectoryComparer: IComparer
    //{
    //    public int Compare(object x, object y)
    //    {
    //        CustomFileSystemInfo first, second;

    //        first = (CustomFileSystemInfo)x;
    //        second = (CustomFileSystemInfo)y;

    //        DirectoryInfo firstDirectory, secondDirectory;
    //        firstDirectory = first.AsDirectory;
    //        secondDirectory = second.AsDirectory;

    //        if (firstDirectory != null && secondDirectory == null)
    //            return -1;

    //        if (secondDirectory != null && firstDirectory == null)
    //            return 1;

    //        if (secondDirectory != null && firstDirectory != null)
    //            return string.Compare(firstDirectory.Name, secondDirectory.Name);

    //        return string.Compare(first.Name, second.Name);
    //    }
    //}
}
